//使用机构活动模板
exports.UseOrganizationDiscountTemplate = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    //任务流创建
    async.waterfall([
            //机构模板活动
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationActivityTemplate + "/oat_id/" + restfulParams.oat_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        if (err) {
                            return cb(err, data);
                        }
                        var duration = data.data[0].oa_class_duration;
                        var duration_split = duration.split("|");
                        resultData.template = data.data;
                        resultData.template[0].course_count = duration_split[0];
                        resultData.template[0].course_time = duration_split[1];
                        cb();
                        cb();
                    }
                )
            },
        function (cb) {
            if (resultData.template[0].oat_skin_id > 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchActivitySkin + "/as_id/" + resultData.template[0].oat_skin_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        if (err) {

                            return cb(err, data);
                        }
                        resultData.skinData = data.data;
                        cb();
                    }
                )

            } else {
                resultData.skinData = [];
                cb()
            }
        },
            //机构模板活动拓展
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOorganizationDiscountsActivityTemplate + "/dat_oatid/" + restfulParams.oat_id,
                        method: 'GET',
                        /*data: {
                            fields: ["ucc_uid","cc_name","cc_start_time","cc_end_time","cc_desc","cc_price"].join(",")
                        }*/
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        resultData.extend = data.data;
                        cb();
                    }
                );
            },
            //机构模板活动内容
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOorganizationDiscountsActivityTemplateContent + "/dac_oatid/" + restfulParams.oat_id + "/sort_order/dac_order-asc",
                        method: 'POST',
                        data: {
                            fields: [
                                "dac_type as type",
                                "dac_content as content"
                            ].join(",")
                        }
                    },
                    function (err, data) {
                        if (err) {
                            return cb(err, data);
                        }
                        resultData.content = data.data;
                        cb();
                    }
                )
            },
            //查询音乐信息
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchActivityMusic + "/am_id/" + resultData.template[0].oat_music_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        if (err) {

                            return cb(err, data);
                        }
                        resultData.musicData = data.data
                        cb();
                    }
                )
            },
            //查询封面信息
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchActivityCover + "/ac_id/" + resultData.template[0].oat_cover_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        if (err) {

                            return cb(err, data);
                        }
                        resultData.coverData = data.data
                        cb();
                    }
                )
            },
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}